package cn.css0209.applicationdemo190509.book.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.css0209.applicationdemo190509.R;
import cn.css0209.applicationdemo190509.book.BookDo;

public class BookAdapter extends BaseAdapter {
    private List<BookDo> bookDos = new ArrayList<>();
    private Context context;
    private LayoutInflater inflater;

    public BookAdapter(Context context){
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    public void addAllData(List<BookDo> bookDos) {
        this.bookDos = bookDos;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return bookDos.size();
    }

    @Override
    public Object getItem(int position) {
        return bookDos.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.list_item_layout, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        BookDo student = bookDos.get(position);
        holder.ivIcon.setImageResource(R.drawable.head3);
        holder.tvName.setText(student.getName());
        holder.tvPhone.setText(student.getPhone());

        return convertView;
    }
    class ViewHolder {
        @BindView(R.id.item_head)
        ImageView ivIcon;
        @BindView(R.id.item_name)
        TextView tvName;
        @BindView(R.id.item_phone)
        TextView tvPhone;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
